package org.zasadnyy.redlions.players.server;

import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.zasadnyy.redlions.players.server.converters.PlayersConverter;
import org.zasadnyy.redlions.players.server.converters.TournamentsConverter;
import org.zasadnyy.redlions.players.server.converters.impl.PlayersConverterImpl;
import org.zasadnyy.redlions.players.server.converters.impl.TournamentConverterImpl;
import org.zasadnyy.redlions.players.server.dao.Dao;
import org.zasadnyy.redlions.players.server.dao.impl.JdoPlayersDao;
import org.zasadnyy.redlions.players.server.dao.impl.JdoTournmaentDao;
import org.zasadnyy.redlions.players.server.model.PlayerObject;
import org.zasadnyy.redlions.players.server.model.TournamentObject;
import org.zasadnyy.redlions.players.shared.model.PlayerItem;
import org.zasadnyy.redlions.players.shared.model.TournamentItem;
import org.zasadnyy.redlions.players.shared.remote.PlayersCrudRemote;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

public class PlayersServlet extends RemoteServiceServlet implements
		PlayersCrudRemote {

	private static final long serialVersionUID = -363237720410861222L;
	private static final Logger LOG = LoggerFactory
			.getLogger(PlayersServlet.class);

	private Dao<PlayerObject> plaeyrDao = new JdoPlayersDao();
	private Dao<TournamentObject> tournamentDao = new JdoTournmaentDao();
	private PlayersConverter playersConverter = new PlayersConverterImpl();
	private TournamentsConverter tournamentsConverter = new TournamentConverterImpl();

	private void addNewPlaeyr() {
		for (int i = 0; i < 10; i++) {
			Set<Key> tournamentKeys = new HashSet<Key>();
			Set<Key> palyerKeys = new HashSet<Key>();

			PlayerObject player = new PlayerObject("Антон", "Ліпенцев",
					"lipar.jpeg", "Львів", new Date(), new Date(),
					tournamentKeys);
			Key playerKey = plaeyrDao.addOrEdit(player);
			palyerKeys.add(playerKey);

			TournamentObject tournamentObject1 = new TournamentObject("ВЧУ",
					"http://ultimate.com.ua/players/lipar/", new Date());
			tournamentObject1.setPlayers(palyerKeys);
			Key tk1 = tournamentDao.addOrEdit(tournamentObject1);
			tournamentKeys.add(tk1);

			TournamentObject tournamentObject2 = new TournamentObject("HAT",
					"http://code.google.com/intl/", new Date());
			tournamentObject2.setPlayers(palyerKeys);
			Key tk2 = tournamentDao.addOrEdit(tournamentObject2);
			tournamentKeys.add(tk2);

			TournamentObject tournamentObject3 = new TournamentObject(
					"Брест Без границь", "link to somewhere", new Date());
			tournamentObject3.setPlayers(palyerKeys);
			Key tk3 = tournamentDao.addOrEdit(tournamentObject3);
			tournamentKeys.add(tk3);

			player.setTournaments(tournamentKeys);
			plaeyrDao.addOrEdit(player);
		}
	}

	// private List<PlayerObject> generatePlaeyrs() {
	// List<PlayerObject> items = new ArrayList<PlayerObject>();
	//
	// for (int i = 0; i < 10; i++) {
	// Set<Key> tournamentKeys = new HashSet<Key>();
	// Set<Key> palyerKeys = new HashSet<Key>();
	//
	// PlayerObject player = new PlayerObject("Anton", "Lipentsev",
	// "lipar.jpeg", "Lviv", new Date(), new Date(),
	// tournamentKeys);
	// Key playerKey = plaeyrDao.addOrEdit(player);
	// palyerKeys.add(playerKey);
	//
	// TournamentObject tournamentObject1 = new TournamentObject("OCHU",
	// "http://ultimate.com.ua/players/lipar/", new Date());
	// tournamentObject1.setPlayers(palyerKeys);
	// Key tk1 = tournamentDao.addOrEdit(tournamentObject1);
	// tournamentKeys.add(tk1);
	//
	// TournamentObject tournamentObject2 = new TournamentObject(
	// "HAT",
	// "http://code.google.com/intl/",
	// new Date());
	// tournamentObject2.setPlayers(palyerKeys);
	// Key tk2 = tournamentDao.addOrEdit(tournamentObject2);
	// tournamentKeys.add(tk2);
	//
	// TournamentObject tournamentObject3 = new TournamentObject("BBG",
	// "link to somewhere", new Date());
	// tournamentObject3.setPlayers(palyerKeys);
	// Key tk3 = tournamentDao.addOrEdit(tournamentObject3);
	// tournamentKeys.add(tk3);
	//
	// player.setTournaments(tournamentKeys);
	// plaeyrDao.addOrEdit(player);
	//
	// items.add(player);
	// }
	//
	// return items;
	// }

	@Override
	public PlayerItem getPlayerById(String id) {
		LOG.info("======getPlayerById");
		if (id == null) {
			return null;
		}
		Key key = KeyFactory.stringToKey(id);
		PlayerObject playerObject = plaeyrDao.getById(key);
		return playersConverter.toItem(playerObject);
	}

	@Override
	public List<PlayerItem> getAllPlayers() {

		LOG.info("======getAllPlayers");

		List<PlayerObject> allPlayers = plaeyrDao.getAll();
		// if (allPlayers.isEmpty()) {
		// addNewPlaeyr();
		// LOG.info("addNewPlayerCalled");
		// }
		// allPlayers = plaeyrDao.getAll();
		return playersConverter.toItemList(allPlayers);
	}

	@Override
	public void addOrEditPlayer(PlayerItem playerItem) {
		PlayerObject playerObject = playersConverter.toObject(playerItem);
		plaeyrDao.addOrEdit(playerObject);
	}

	@Override
	public void deletePlayerById(String id) {
		LOG.info("======deletePlayerById");
		if (id == null) {
			return;
		}
		Key key = KeyFactory.stringToKey(id);
		plaeyrDao.deleteById(key);
	}

	@Override
	public List<TournamentItem> getAllTournaments() {
		LOG.info("======getAllTournaments");

		List<TournamentObject> allPlayers = tournamentDao.getAll();
		return tournamentsConverter.toItemList(allPlayers);
	}

	@Override
	public void addOrEditTournment(TournamentItem tournament) {
		TournamentObject object = tournamentsConverter.toObject(tournament);
		tournamentDao.addOrEdit(object);
	}

	@Override
	public void deleteTournamentById(String id) {
		if (id == null) {
			return;
		}
		Key key = KeyFactory.stringToKey(id);
		tournamentDao.deleteById(key);
	}

}
